package fm.dongman.animefm.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.animefm.view.util.ActivityHelper;
import fm.dongman.modellib.model.SheetModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 发现》漫单列表
 * Created by Administrator on 2017/10/15.
 */

public class FSheetListAdapter extends RecyclerView.Adapter<FSheetListAdapter.ViewHolder> {
    public static final int TYPE_MANDAN = 1;
    public static final int TYPE_TOPIC = 2;

    private Context mContext;
    private List<IModel.ISheetModel> mListSheet;
    private int mType;

    public FSheetListAdapter(List<IModel.ISheetModel> listSheet, Context context) {
        this(listSheet, context, TYPE_MANDAN);
    }

    public FSheetListAdapter(List<IModel.ISheetModel> listSheet, Context context, int type) {
        mContext = context;
        mListSheet = listSheet;
        mType = type;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_find_sheet, null);
        return new ViewHolder(view);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvSheetImg;
        private TextView mSheetTitle;
        private ImageView mAvatar;
        private TextView mUserName;

        public ViewHolder(View itemView) {
            super(itemView);
            mIvSheetImg = (ImageView) itemView.findViewById(R.id.mIvSheetAvatarUrl);
            mSheetTitle = (TextView) itemView.findViewById(R.id.mTvSheetTitle);
            mAvatar = (ImageView) itemView.findViewById(R.id.mIvSheetAvatar);
            mUserName = (TextView) itemView.findViewById(R.id.mTvSheetUserName);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final SheetModel sheetModel = (SheetModel) mListSheet.get(position);
        Glide.with(mContext)
                .load(sheetModel.getMandanImg())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 2)).into(holder.mIvSheetImg);
        holder.mSheetTitle.setText(sheetModel.getMandanTitle());
        if (sheetModel.getUser() != null) {
            Glide.with(mContext)
                    .load(sheetModel.getUser().getAvatarUrl())
                    .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 7)).into(holder.mAvatar);
            holder.mUserName.setText(sheetModel.getUser().getUserName());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(mContext instanceof Activity)) {
                    throw new RuntimeException(mContext.toString() + " 必须是Activity!");
                } else {
                    // 根据类型打开详情页
                    if (mType == TYPE_MANDAN) {
                        Intent intent = new Intent(mContext, SingleActivity.class);
                        intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_SHEET_INFO());
                        intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), sheetModel.getMandanId());
                        ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                        return;
                    }
                    Intent intent = new Intent(mContext, SingleActivity.class);
                    intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_SPECIAL_INFO());
                    intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), sheetModel.getMandanId());
                    ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListSheet.size();
    }


}
